package br.com.wepa.webapps.orca.logica.negocio.grupos;
import static br.com.wepa.webapps.orca.logica.persistencia.QueriesConstants.QUERY_SelectGruposByNivel;
import static br.com.wepa.webapps.orca.util.CheckValue.Condition.NOTEMPTYSTRING;
import static br.com.wepa.webapps.orca.util.CheckValue.Condition.NOTNULL;
import static br.com.wepa.webapps.orca.util.CheckValue.Condition.NOTZERO;
import br.com.wepa.webapps.logger.TraceLogger;
import br.com.wepa.webapps.orca.logica.modelo.Grupo;
import br.com.wepa.webapps.orca.logica.negocio.BusinessConstants;
import br.com.wepa.webapps.orca.logica.negocio.BusinessException;
import br.com.wepa.webapps.orca.logica.negocio.facade.to.GrupoTO;
import br.com.wepa.webapps.orca.logica.persistencia.DAOFactory;
import br.com.wepa.webapps.orca.logica.persistencia.GrupoDAO;
import br.com.wepa.webapps.orca.logica.persistencia.search.HqlQuerySearch;
import br.com.wepa.webapps.orca.logica.persistencia.search.NamedQuerySearch;
import br.com.wepa.webapps.orca.logica.persistencia.transaction.TransactionManager;
import br.com.wepa.webapps.orca.util.bean.BeanHelperUtil;

class GruposUtil extends TransactionManager{
	private static final TraceLogger logger = new TraceLogger(GruposUtil.class);
	private static GrupoDAO dao = DAOFactory.instance(DAOFactory.HIBERNATE).getGrupoDAO();
	
	/**
	 * Faz setup de um grupo e seta no TO o nivel da instancia
	 * @param to
	 * @return
	 * @throws BusinessException
	 */
	
	static GrupoTO setUpByNivel(GrupoTO to) throws BusinessException {
		logger.entering();
		try {
			if (to.getUpdate() && to.getId() != null) {
				Grupo model = dao.findById(to.getId(), to.getLock());
				BeanHelperUtil.copyProperties(to, model);
				GruposManager.setNivelTO(to, model);
			}
			if (to.isEnabledSearcher()) {
				to.getSearcher().executeAll();
			}
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(), e);
		}
		logger.exiting();
		return to;
	}
	
	/**
	 * Carrega um grupo e seta o nivel no to
	 * @param to
	 * @param lock
	 * @return
	 * @throws BusinessException
	 */
	static GrupoTO loadByNivel(GrupoTO to, boolean lock) throws BusinessException {		
		logger.entering();
		Grupo model = null;
		try {
			Integer modelID = to.getId();

			if (modelID == null)
				throw new BusinessException(BusinessConstants.ERROR_REGISTRO_NAO_ENCONTRADO);

			model = dao.findById(modelID, lock);
			if (model == null) {
				throw new BusinessException(BusinessConstants.ERROR_REGISTRO_NAO_ENCONTRADO);
			}
			dao.refresh(model);
			
			GruposManager.setNivelTO(to, model);
		
			BeanHelperUtil.copyProperties(to, model);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(), e);
		}
		logger.exiting();
		return to;
	}
	
	/**
	 * Pesquisa um grupo 
	 * @param to
	 * @return
	 * @throws BusinessException
	 */
	 static GrupoTO find(GrupoTO to) throws BusinessException {
		logger.entering();
		try {		
			// Polymorphic  Query
			HqlQuerySearch<Grupo> search =  new HqlQuerySearch<Grupo>(to.getPagingBean());			
			search.from("Grupo");				
			search.and("nivel = ? ", to.getNivel());
			search.andIf("idGrupo = ? ", to.getIdGrupo(), NOTNULL, NOTZERO);
			search.andIf("descricao = ? ", to.getDescricao(), NOTNULL, NOTEMPTYSTRING);
			search.andIf("idPai = ? ", to.getIdPai(), NOTNULL, NOTZERO);
			search.andIf("nome = ? ", to.getNome(), NOTNULL, NOTEMPTYSTRING);
			search.orderBy("nome,idGrupo");
			search.find();
			search.disableFind();
			to.getSearcher().addSearch(search);			
			to.setShowSearchResults(true);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(), e);
		}
		logger.exiting();
		return to;
	}
		
	

	/**
	 * Encontra os grupos por nivel
	 * @param to
	 * @return
	 * @throws BusinessException
	 */
	static GrupoTO findAllByNivel(GrupoTO to) throws BusinessException {
		logger.entering();
		try {		
			NamedQuerySearch<Grupo> search =  new NamedQuerySearch<Grupo>(QUERY_SelectGruposByNivel, to.getNivel());
			search.configPaging(to.getPagingBean());
			search.find();
			search.disableFind();
			to.getSearcher().addSearch(search);			
			to.setShowSearchResults(true);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(), e);
		}
		logger.exiting();
		return to;
	}
}
